<?php

namespace Console\Seeders;

use Exception;
use Illuminate\Support\Facades\DB;

class AdminInitSeeder
{
    public function run()
    {
        $infoRs = DB::table('system_user')->select('id')
            ->orderBy('id', 'asc')->first();
        if (isset($infoRs->id)) {
            return;
        }

        DB::beginTransaction();
        try {
            $roleId = $this->generateAdminRole();

            $this->generateAdminUser($roleId);

            $this->generateMenu();

            DB::commit();
        } catch (Exception $e) {
            DB::rollBack();

            throw new Exception($e->getMessage());
        }
    }

    private function generateAdminRole(): int
    {
        $nowTime = date('Y-m-d H:i:s');

        $insertData = [
            'title' => '系统管理员',
            'describe' => '管理系统的超级管理角色',
            'status' => 1,
            'created_at' => $nowTime,
        ];
        $roleId = DB::table('system_role')->insertGetId($insertData);
        if ($roleId <= 0) {
            throw new Exception('管理角色生成错误');
        }

        $nodeRs = [
            'App\Controllers\Admin\System\AuthController:index',
            'App\Controllers\Admin\System\AuthController:edit',
            'App\Controllers\Admin\System\AuthController:state',
            'App\Controllers\Admin\System\AuthController:node',

            'App\Controllers\Admin\System\UserController:index',
            'App\Controllers\Admin\System\UserController:edit',
            'App\Controllers\Admin\System\UserController:state',

            'App\Controllers\Admin\System\ConfigController:system',

            'App\Controllers\Admin\System\MenuController:index',
            'App\Controllers\Admin\System\MenuController:edit',
            'App\Controllers\Admin\System\MenuController:icon',
            'App\Controllers\Admin\System\MenuController:state',
        ];
        $insertData = [];
        foreach ($nodeRs as $v) {
            $insertData[] = [
                'role_id' => $roleId,
                'node' => $v,
                'created_at' => $nowTime,
            ];
        }
        DB::table('system_role_node')->insert($insertData);

        return $roleId;
    }

    private function generateAdminUser(int $roleId)
    {
        $nowTime = date('Y-m-d H:i:s');

        $insertData = [
            'username' => 'administrator',
            'nickname' => '管理员',
            'password' => password_hash('123456', PASSWORD_DEFAULT),
            'status' => 1,
            'created_at' => $nowTime,
        ];
        $userId = DB::table('system_user')->insertGetId($insertData);
        if ($userId <= 0) {
            throw new Exception('管理员生成错误');
        }

        $insertData = [
            'role_id' => $roleId,
            'user_id' => $userId,
            'created_at' => $nowTime,
        ];
        DB::table('system_role_user')->insert($insertData);
    }

    private function generateMenu()
    {
        $nowTime = date('Y-m-d H:i:s');

        $insertData = [
            'pid' => 0,
            'title' => '系统管理',
            'icon' => 'far fa-circle',
            'node' => '#',
            'status' => 1,
            'created_at' => $nowTime,
        ];

        $insertId = DB::table('system_menu')->insertGetId($insertData);
        if ($insertId <= 0) {
            throw new Exception('一级菜单生成错误');
        }

        $insertData['pid'] = $insertId;
        $insertData['title'] = '系统配置';
        $insertData['icon'] = 'fab fa-whmcs';
        $insertId = DB::table('system_menu')->insertGetId($insertData);
        if ($insertId <= 0) {
            throw new Exception('二级菜单生成错误');
        }

        $insertData = [
            [
                'pid' => $insertId,
                'title' => '系统参数',
                'icon' => 'fab fa-lastfm',
                'node' => '/admin/system/config/system',
                'status' => 1,
                'created_at' => $nowTime,
            ],
            [
                'pid' => $insertId,
                'title' => '系统菜单',
                'icon' => 'fas fa-list-ul',
                'node' => '/admin/system/menu/index',
                'status' => 1,
                'created_at' => $nowTime,
            ],
            [
                'pid' => $insertId,
                'title' => '访问权限组',
                'icon' => 'fab fa-uncharted',
                'node' => '/admin/system/auth/index',
                'status' => 1,
                'created_at' => $nowTime,
            ],
            [
                'pid' => $insertId,
                'title' => '系统用户',
                'icon' => 'fab fa-hornbill',
                'node' => '/admin/system/user/index',
                'status' => 1,
                'created_at' => $nowTime,
            ],
        ];

        DB::table('system_menu')->insert($insertData);
    }
}
